//
//  GameState.swift
//  Loop
//
//  Created by remy on 2021/9/6.
//

import GameplayKit

class ReadyState: GKState {
    unowned let scene: StageScene
    
    init(scene: StageScene) {
        self.scene = scene
        super.init()
    }
    
    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        return stateClass is PlayState.Type
    }
    
    override func didEnter(from previousState: GKState?) {
        scene.loadReadyUI(gameOver: previousState is PlayState)
    }
    
    override func willExit(to nextState: GKState) {
        scene.childNode(withName: "readyWrap")?.removeFromParent()
    }
}

class PlayState: GKState {
    unowned let scene: StageScene
    
    init(scene: StageScene) {
        self.scene = scene
        super.init()
    }
    
    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        return stateClass is PauseState.Type || stateClass is ReadyState.Type
    }
    
    override func didEnter(from previousState: GKState?) {
        if previousState is ReadyState {
            scene.loadPlayUI()
        } else {
            scene.gameResume()
        }
    }
}

class PauseState: GKState {
    unowned let scene: StageScene
    
    init(scene: StageScene) {
        self.scene = scene
        super.init()
    }
    
    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        return stateClass is PlayState.Type
    }
    
    override func didEnter(from previousState: GKState?) {
        scene.gamePause()
    }
}
